
# ------------------------------------------------------------------------------------------------
### Matrix Completion Estimator Functions
# ------------------------------------------------------------------------------------------------

# Matrix completion estimate function
mc_estimate = function(Y, N0, T0) {
  N1=nrow(Y)-N0
  T1=ncol(Y)-T0
  W <- outer(c(rep(0,N0),rep(1,N1)),c(rep(0,T0),rep(1,T1)))
  mc_pred <- mcnnm_cv(Y, 1-W, num_lam_L = 20)
  mc_fit  <- mc_pred$L + outer(mc_pred$u, mc_pred$v, '+')
  mc_est <- sum(W*(Y-mc_fit))/sum(W)
  mc_est
}

# Matrix completion placebo standard error function
mc_placebo_se = function(Y, N0, T0, replications=200) {
  N1 = nrow(Y) - N0
  theta = function(ind) { mc_estimate(Y[ind,], length(ind)-N1, T0) }
  sqrt((replications-1)/replications) * sd(replicate(replications, theta(sample(1:N0))))
}